package com.toidep.fragment.mains;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemClickListener;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.adapters.AlbumAdapter;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.interfaces.IFragmentRefreshable;
import com.toidep.widgets.sectionlist.AmazingListView;

public class AlbumFragment extends BaseFragment implements OnClickListener, IFragmentRefreshable{

	private String TAG = "MainFragment";
	private final int TAB_NEW = 1;
	private final int TAB_HOT = 2;
	private final int TAB_TOP = 3;
	private final int TAB_ALL = 0;

	public boolean isRootFragment = true;
	private MainActivity mainActivity;
	private View view;
	
	private RelativeLayout buttonNew, buttonHot, buttonTop, buttonAll;
	private RelativeLayout layoutNew, layoutHot, layoutTop, layoutAll;
	
	private AmazingListView listAlbumsNew, listAlbumsHot, listAlbumsTop, listAlbumsAll;
	private AlbumAdapter adapterAlbumsNew, adapterAlbumsHot, adapterAlbumsTop, adapterAlbumsAll;

	private boolean isRequestedNewData = false;
	private boolean isRequestedHotData = false;
	private boolean isRequestedTopData = false;
	private boolean isRequestedAllData = false;
	
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_ALBUM;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_album_menu));
		mainActivity.mainFragment.showButtonSlide(true);
		
		isRequestedNewData = false;
		isRequestedHotData = false;
		isRequestedTopData = false;
		isRequestedAllData = false;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_album, container, false);

		mainActivity = (MainActivity) getActivity();
		
		initComponents();
		
		switchMenuAlbum(TAB_NEW);
		
		setBaseValues();
		
		return view;
	}

	private void initComponents() {
		
		buttonNew = (RelativeLayout) view.findViewById(R.id.layoutButtonNewOfAlbums);
		buttonNew.setOnClickListener(this);
		buttonHot = (RelativeLayout) view.findViewById(R.id.layoutButtonHotOfAlbums);
		buttonHot.setOnClickListener(this);
		buttonTop = (RelativeLayout) view.findViewById(R.id.layoutButtonTopOfAlbums);
		buttonTop.setOnClickListener(this);
		buttonAll = (RelativeLayout) view.findViewById(R.id.layoutButtonAllOfAlbums);
		buttonAll.setOnClickListener(this);
		
		layoutNew = (RelativeLayout) view.findViewById(R.id.layoutAlbumNew);
		layoutHot = (RelativeLayout) view.findViewById(R.id.layoutAlbumHot);
		layoutTop = (RelativeLayout) view.findViewById(R.id.layoutAlbumTop);
		layoutAll = (RelativeLayout) view.findViewById(R.id.layoutAlbumAll);
	}
	
	public static AlbumFragment newInstance() {
		AlbumFragment myFragment = new AlbumFragment();

		myFragment.setRootFragment(true);
		return myFragment;
	}
	
	public void switchMenuAlbum(int tabId) {
		
		switch (tabId) {
		case TAB_NEW:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_red_normal);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.VISIBLE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.GONE);
			break;
			
		case TAB_HOT:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_red_normal);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.VISIBLE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.GONE);
			break;
			
		case TAB_TOP:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_red_normal);
			buttonAll.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.VISIBLE);
			layoutAll.setVisibility(View.GONE);
			break;
			
		case TAB_ALL:
			// Menu color
			buttonNew.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonHot.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonTop.setBackgroundResource(R.drawable.bg_horizontal_button_menu);
			buttonAll.setBackgroundResource(R.drawable.bg_red_normal);
			
			// Layout content
			layoutNew.setVisibility(View.GONE);
			layoutHot.setVisibility(View.GONE);
			layoutTop.setVisibility(View.GONE);
			layoutAll.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
		
		requestData(tabId);
		setPaddingAllButton();
		
	}
	
	private void setPaddingAllButton() {
		int paddingDP = (int)(10 * ConfigUtility.scale);
		buttonNew.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonHot.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonTop.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		buttonAll.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
	}
	
	/**
	 * Goi API get list models
	 */
	private void requestData(int tabId) {
		switch (tabId) {
		case TAB_NEW:
			if (isRequestedNewData)
				return;
			
			isRequestedNewData = true;
			// Khoi tao list model New
			listAlbumsNew = (AmazingListView) view.findViewById(R.id.listAlbumNew);
			listAlbumsNew.setAdapter(adapterAlbumsNew = new AlbumAdapter(this, Constants.CATEGORY_ALBUM, tabId));
			listAlbumsNew.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterAlbumsNew.setAmazingListView(listAlbumsNew, ConfigUtility.PAGE_SIZE);
			adapterAlbumsNew.notifyMayHaveMorePages();
			
			// Chuyen toi detail
			listAlbumsNew.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterAlbumsNew != null && position >= 0) {
						adapterAlbumsNew.onItemClick(position);
					}
				}
			});
			
			adapterAlbumsNew.resetList();
			break;
		case TAB_HOT:
			if (isRequestedHotData)
				return;
			
			isRequestedHotData = true;
			// Khoi tao list model Hot
			listAlbumsHot = (AmazingListView) view.findViewById(R.id.listAlbumHot);
			listAlbumsHot.setAdapter(adapterAlbumsHot = new AlbumAdapter(this, Constants.CATEGORY_ALBUM, tabId));
			listAlbumsHot.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterAlbumsHot.setAmazingListView(listAlbumsHot, ConfigUtility.PAGE_SIZE);
			adapterAlbumsHot.notifyMayHaveMorePages();
			
			// Chuyen toi detail
			listAlbumsHot.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterAlbumsHot != null && position >= 0) {
						adapterAlbumsHot.onItemClick(position);
					}
				}
			});
			
			adapterAlbumsHot.resetList();
			break;
		case TAB_TOP:
			if (isRequestedTopData)
				return;
			
			isRequestedTopData = true;
			// Khoi tao list model Top
			listAlbumsTop = (AmazingListView) view.findViewById(R.id.listAlbumTop);
			listAlbumsTop.setAdapter(adapterAlbumsTop = new AlbumAdapter(this, Constants.CATEGORY_ALBUM, tabId));
			listAlbumsTop.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterAlbumsTop.setAmazingListView(listAlbumsTop, ConfigUtility.PAGE_SIZE);
			adapterAlbumsTop.notifyMayHaveMorePages();
			
			// Chuyen toi detail
			listAlbumsTop.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterAlbumsTop != null && position >= 0) {
						adapterAlbumsTop.onItemClick(position);
					}
				}
			});
			
			adapterAlbumsTop.resetList();
			break;
		case TAB_ALL:
			if (isRequestedAllData)
				return;
			
			isRequestedAllData = true;
			// Khoi tao list model All
			listAlbumsAll = (AmazingListView) view.findViewById(R.id.listAlbumAll);
			listAlbumsAll.setAdapter(adapterAlbumsAll = new AlbumAdapter(this, Constants.CATEGORY_ALBUM, tabId));
			listAlbumsAll.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
			adapterAlbumsAll.setAmazingListView(listAlbumsAll, ConfigUtility.PAGE_SIZE);
			adapterAlbumsAll.notifyMayHaveMorePages();
			
			// Chuyen toi detail
			listAlbumsAll.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					if (adapterAlbumsAll != null && position >= 0) {
						adapterAlbumsAll.onItemClick(position);
					}
				}
			});
			
			adapterAlbumsAll.resetList();
			break;
		}
	}

	@Override
	public void onClick(View v) {

		if (v == buttonNew) {
			switchMenuAlbum(TAB_NEW);
		} else if (v == buttonHot) {
			switchMenuAlbum(TAB_HOT);
		} else if (v == buttonTop) {
			switchMenuAlbum(TAB_TOP);
		} else if (v == buttonAll) {
			switchMenuAlbum(TAB_ALL);
		}
		
	}

	@Override
	public void refreshData() {
		// TODO Auto-generated method stub
		if ((adapterAlbumsNew != null) && adapterAlbumsNew.getCount() > 0) {
			adapterAlbumsNew.resetList();
		}
		if ((adapterAlbumsHot != null) && adapterAlbumsHot.getCount() > 0) {
			adapterAlbumsHot.resetList();
		}
		if ((adapterAlbumsTop != null) && adapterAlbumsTop.getCount() > 0) {
			adapterAlbumsTop.resetList();
		}
		if ((adapterAlbumsAll != null) && adapterAlbumsAll.getCount() > 0) {
			adapterAlbumsAll.resetList();
		}
	}

}
